// Rects and Rooms are used in dungeon generation, see dungeon.h/.c
#ifndef RECT_H_
#define RECT_H_

#define RECTLIST_MAX 32

typedef struct {
	int left;
	int top;
	int right;
	int bottom;
	int z;
} Rect;

// Space is like a Rect, with depth
typedef struct {
	int xbegin, xend; // begin is included in space
	int ybegin, yend; // end is excluded from space
	int zbegin, zend;
} Space;

typedef struct {
	Rect rect [RECTLIST_MAX];
	int n_rects;
} RectList;

typedef struct { 
	int x,y,z;
} Coord;

void cut_all_rects (RectList* list, const Rect* cutout, int inc_x, int inc_y);
int point_in_rect (const Rect* r, int x, int y, int z);
int rect_intersect_rect (const Rect* r1, const Rect* r2);
Coord rect_middle (Rect* r);

#define COORDS_EQUAL(C1,C2) (((C1).x==(C2).x) && ((C1).y==(C2).y) && ((C1).z==(C2).z))

/* Expands a coord into an xyz tuple */
#define XYZ(C) (C).x, (C).y, (C).z

/*
 * turns coords into an index for a 3-dimensional tile array like the
 * ones in Layout and also Map. CUBE is a pointer to an object with a 
 * tile width and height (i.e. Layout or Map). X, Y, Z are the
 * coordinates to get the index for.
 */
#define TILE_INDEX(CUBE,X,Y,Z) ( \
	assert(((X) < (CUBE)->width) && ((Y) < (CUBE)->height)), \
	(Z)*(CUBE)->width*(CUBE)->height + (Y)*(CUBE)->width + (X) \
)

/* turns coords into an index for a 3-dimensional tile array */
#define TILEC_INDEX(CUBE,C) TILE_INDEX(CUBE,(C).x,(C).y,(C).z)

/* accesses the tile at the specified coords. Works on map and layout ptrs. */
#define TILE(OBJ,X,Y,Z) ((OBJ)->tile[TILE_INDEX(OBJ,X,Y,Z)])

/* accesses the tile at the specified coords. Works on map and layout ptrs. */
#define TILEC(OBJ,C) TILE(OBJ,(C).x,(C).y,(C).z)

/* manhattan distance between two coords, given as x/y/z components */
#define DIST(X1,Y1,Z1,X2,Y2,Z2) (abs((X2)-(X1))+abs((Y2)-(Y1))+abs((Z2)-(Z1)))

/* manhattan distance between two coords, as Coord for convenience */
#define DISTC(C1,C2) DIST((C1).x,(C1).y,(C1).z,(C2).x,(C2).y,(C2).z)

#endif
